<?php
/**
 * 《车辆管理》实用类
 *
 * @author 孙晓晔
 * @version $Id: CarUtil.class.php 1035 2010-03-26 05:24:59Z sunxy $
 */
require_once(dirname(__FILE__) . '/../AdminUtil.class.php');

class CarUtil extends AdminUtil {
	
	public $address = array(
		'-' => '请选择',
		
		'南山南园枫叶至沙井工厂' => '南山南园枫叶至沙井工厂',
		'南山南园枫叶至清溪工厂' => '南山南园枫叶至清溪工厂',
		'沙井工厂至清溪工厂' => '沙井工厂至清溪工厂',
		'清溪工厂至沙井工厂' => '清溪工厂至沙井工厂',
		'清溪工厂至南山南园枫叶' => '沙井工厂至南山南园枫叶',
	);

	function __construct() {
		$request['update'] = '$Date: 2009/03/03 07:24:42 $';
		$request['revision'] = '$Revision: 1.5 $';
			
		parent::__construct($request);
		
		$this->t = array_merge($this->t, $this->b);
	}
	
	public $b = array(
		'car'=>'请输入车辆编号，然后选择并回车即可。',
		'driver'=>'请输入司机姓名拼音首字母，然后选择并回车即可。如张三，输入zs',
		''=>'请输入派车人姓名拼音首字母，然后选择并回车即可。如张三，输入zs',
	);

	/**
	 * 预处理
	 *
	 * @param Controler $controller
	 * @param array $request
	 */
	function pretreat($controller) {
		parent::pretreat($controller);

		$controller->assign_by_ref('address_option', $this->address);
		
		// $controller->assign_by_ref('t', $this->title);
	}

	/**
	 * 删除
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function delete($controller, $request) {
		$this->pretreat($controller);

		$db = newdb($this->dsn);
		while (list($k, $id) = each($request)) {
			if (substr($k, 0, 2) <> "c_") {
				continue;
			}

			$fmt = "DELETE FROM %s WHERE id = '%s'";
			$sql = sprintf($fmt, $this->tbl, $id);
			// debug($sql);
			$db->query($sql);
		}
		
		$controller->assign('baseLink', $this->doGet($request, 'findPost'));

		$this->findPost($controller, $request);
		return true;
	}

	function status($controller, $request) {
		$this->pretreat($controller);

		$id = $request['id'];
		clean($this->tbl . "_id_" . $id);

		$status = $request['status'];

		$db = newdb($this->dsn);
		$fmt = "UPDATE %s SET status = '%s', modifytime = now() WHERE id = '%s'";
		$sql = sprintf($fmt, $this->tbl, $status, $id);
		// debug($sql);
		$db->query($sql);
		
		$controller->assign('baseLink', $this->doGet($request, 'findPost'));

		$this->findPost($controller, $request);
		return true;
	}

	function _date($t1, $t2) {
		$t1 = strtotime($t1);
		$t2 = strtotime($t2);
		
		$date1 = date("Y-m-d", $t1);
		$date2 = date("Y-m-d", $t2);		
		if ($date1 == $date2) {
			return sprintf("%s(%s~%s)", $date1, date("H:i", $t1), date("H:i", $t2));
		}
		
		return sprintf("%s至%s", date("Y-m-d H:i", $t1), date("Y-m-d H:i", $t2));
	}
	
	function _time($t1, $t2) {
		$t1 = strtotime($t1);
		$t2 = strtotime($t2);
		
		return sprintf("%s~%s", date("H:i", $t1), date("H:i", $t2));		
	}
}